<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>数字人对话</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=0">

    <link href="/static/css/avatar.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdn.novadata.top/css/weui.min.css" >
    <style>
        .warning-counter {
            position: fixed;
            top: 20px;
            left: 20px;
            background: #ffc107;
            padding: 10px;
            border-radius: 4px;
            font-size: 18px; /* 增加字体大小 */
            font-weight: bold; /* 加粗字体 */
        }
        #preview {
            width: 300px;
            position: fixed;
            top: 20px;
            right: 20px;
            border: 2px solid #007bff;
            border-radius: 4px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            z-index: 9999; /* 新增关键属性 */
        }
        .end-camera-button, .start-camera-button {
            margin-top: 20px;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
    </style>
</head>
<body>

<div class="warning-counter">警告次数：<span id="warningCount">0</span>/10</div>

<div id="container"></div>
<div id="subtitlesContainer" style="display: none"><span id="subtitles" class="scroll-text">Hello</span></div>
<a href="#" role="button" id="startChat" class="weui-btn weui-btn_primary disabled" >开始</a>
<div id="controls" style="display: none">
<!--    <label id="status" style="color: white;margin-right: 20px" ></label>-->
    <img id="waveform" src="https://cdn.novadata.top/img/waveform3.gif" alt="wave" style="display: none"/>
    <button id="mute"><i class="material-icons">mic_off</i></button>
    <button id="play"><i class="material-icons">play_arrow</i></button>
    <button id="subtitleBtn"><i class="material-icons">subtitles_off</i></button>
</div>
<video id="preview" autoplay muted playsinline></video>
<button class="end-camera-button" id="endCameraButton" style="display: none">终止摄像</button>
<button class="start-camera-button hidden" id="startCameraButton" style="display: none">开始摄像</button>
<script>
        // 防作弊相关变量
        let mediaStream = null;
        let warningCount = 0;
        let isInterviewEnded = false;
        let isCameraActive = true; // 摄像头是否活跃

        // 初始化媒体设备（仅开启摄像头）
        async function initializeMedia() {
            try {
                mediaStream = await navigator.mediaDevices.getUserMedia({
                    video: {
                        width: 800,
                        height: 600,
                        facingMode: 'user'  // 强制使用前置摄像头
                    }
                });

                const videoElement = document.getElementById('preview');
                videoElement.srcObject = mediaStream;
                videoElement.style.transform = 'scaleX(-1)'; // 添加镜像翻转

                return true;
            } catch (error) {
                alert('必须启用摄像头才能进行面试！');
                window.location.href = '/dashboard';
                return false;
            }
        }

        // 启动面试
        async function startInterview() {
            const mediaGranted = await initializeMedia();
            if (!mediaGranted) return;

            alert('请确保全程正面面对摄像头！');
        }

        // 切屏检测
        document.addEventListener('visibilitychange', () => {
            if (document.visibilityState === 'hidden' && !isInterviewEnded) {
                handleCheating('检测到切换屏幕！');
            }
        });

        // 监听窗口大小变化
        let lastWidth = window.innerWidth;
        let lastHeight = window.innerHeight;

        window.addEventListener('resize', () => {
            const currentWidth = window.innerWidth;
            const currentHeight = window.innerHeight;

            // 检查窗口大小，若变小到一定比例（例如小于原来的80%），则计数
            if (currentWidth < lastWidth * 0.8 || currentHeight < lastHeight * 0.8) {
                handleCheating('检测到窗口缩小，可能的切屏行为！');
            }
            lastWidth = currentWidth;
            lastHeight = currentHeight;
        });

        // 处理作弊检测
        function handleCheating(message) {
            warningCount++;
            document.getElementById('warningCount').textContent = warningCount;

            if (warningCount >= 10) {
                endInterview('检测到过多的切屏，面试结束！');
            } else {
                alert(message);
            }
        }

        // 结束面试
        function endInterview(message) {
            isInterviewEnded = true;
            alert(message);
            if (mediaStream) {
                mediaStream.getTracks().forEach(track => track.stop());
            }
            window.location.href = '/dashboard'; // 重定向到结束面试的页面
        }

        // 终止摄像按钮事件
        document.getElementById('endCameraButton').addEventListener('click', () => {
            const userConfirmed = confirm("结束摄像可能会导致终止面试，请确认是否结束摄像");
            if (userConfirmed) {
                if (mediaStream) {
                    mediaStream.getTracks().forEach(track => track.stop());
                    isCameraActive = false; // 摄像头已停用
                    document.getElementById('endCameraButton').classList.add('hidden'); // 隐藏停止按钮
                    document.getElementById('startCameraButton').classList.remove('hidden'); // 显示开始按钮
                    alert('摄像头已结束，仍在进行面试。');
                }
            }
        });

        // 开始摄像按钮事件
        document.getElementById('startCameraButton').addEventListener('click', async () => {
            if (!isCameraActive) {
                const mediaGranted = await initializeMedia();
                if (mediaGranted) {
                    isCameraActive = true; // 恢复摄像头
                    document.getElementById('startCameraButton').classList.add('hidden'); // 隐藏开始按钮
                    document.getElementById('endCameraButton').classList.remove('hidden'); // 显示停止按钮
                }
            }
        });

        // 启动面试
        startInterview();
    </script>

<script>

const fps = 60;

let mapper = {
    0: 67, 1: 40, 2: 44, 3: 46, 4: 42, 5: 22, 6: 24, 7: 68, 8: 41, 9: 45, 10: 47,
    11: 43, 12: 23, 13: 25, 14: 26, 15: 27, 16: 28, 17: 51, 18: 52, 19: 53, 20: 31, 21: 38, 22: 39,
    23: 64, 24: 65, 25: 29, 26: 30, 27: 54, 28: 55, 29: 56, 30: 57, 31: 58, 32: 59, 33: 32, 34: 33,
    35: 60, 36: 61, 37: 36, 38: 37, 39: 62, 40: 63, 41: 17, 42: 18, 43: 19, 44: 20, 45: 21, 46: 48,
    47: 49, 48: 50, 49: 34, 50: 35, 51: 66, 52: -1, 53: -1, 54: -1
}

    //讯飞
    let APP_ID=''
    let API_KEY='';
    let API_SECRET='';

    let role='';
    let openid='';
    {#let model_path="https://cdn.novadata.top/model/role_yskg"#}
    {#['role_jcns','role_yskg','role_rchl','role_tfls'] #}
    let model_path="/static/js/role_yskg.glb"

    let gltf,scene,model,camera,clock;
    let mixer,mixer_idle;
    let audioProcesser,AnimationClip ,AnimationUtils,clipAction_idle_eye,clipAction_idle_head;
    let _data;
</script>
<script src="https://cdn.novadata.top/howler/2.2.3/howler.core.min.js"></script>

<script src="https://cdn.novadata.top/js/iat/crypto-js.js" ></script>

<script src="https://cdn.novadata.top/js/iat/index.umd.js" ></script>

<script src="https://cdn.novadata.top/vConsole/3.15.1/vconsole.min.js"></script>

<script type="module" src="/static/js/avatar.js"></script>
</body>
</html>
